package cat.inkubator.plugin4j.xml;

import java.util.ArrayList;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.JAXBException;
/**
 *
 * @author gato
 */
@XmlRootElement
public class Plugin {

    private String groupId;
    private String artifactId;
    private String author;
    private String version;
    private String urlDownload;
    private String packaging;
    private Plugins dependencies;
    
    public Plugin(){}
    
    public Plugin( String groupId, String artifactId, String author, String version, String urlDownload,
            String packaging, Plugins dependencies){
        
            this.groupId = groupId;
            this.artifactId = artifactId;
            this.author = author;
            this.version = version;
            this.urlDownload = urlDownload;
            this.packaging = packaging;
            this.dependencies = dependencies;
    }

    public String toXML() throws JAXBException{
        Xmlizer xml = new Xmlizer( Plugin.class );
        
        return xml.toXml(this);
    }
    
    public void fromXML( String xmlString ) throws JAXBException{
        Xmlizer xml =  new Xmlizer(Plugin.class);
        
        Plugin plugin = (Plugin) xml.fromXml(xmlString);
        
        this.groupId = plugin.getGroupId();
        this.artifactId = plugin.getArtifactId();
        this.author = plugin.getAuthor();
        this.version = plugin.getVersion();
        this.urlDownload = plugin.getUrlDownload();
        this.packaging = plugin.getPackaging();
        this.dependencies = plugin.getDependencies();
    }

    public boolean hasDependencies(){
        if( dependencies.getPlugins().size() > 0 ){
            return true;
        }
        
        return false;
    }

    @Override
    public boolean equals(Object plugin){
        return true;
    }

    public String obtainFileName(){
        return this.getArtifactId() + "-" + this.getVersion() + "." + this.getPackaging();
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public String getUrlDownload() {
        return urlDownload;
    }

    public void setUrlDownload(String urlDownload) {
        this.urlDownload = urlDownload;
    }

    public Plugins getDependencies() {
        return dependencies;
    }

    public void setDependencies(Plugins dependencies) {
        this.dependencies = dependencies;
    }

    public String getPackaging() {
        return packaging;
    }

    public void setPackaging(String packaging) {
        this.packaging = packaging;
    }
    
    public String toString(){
        
        return this.artifactId + ", " + this.groupId + ", " + this.author + " - " + this.version;
    }

    /**
     * @return the groupId
     */
    public String getGroupId() {
        return groupId;
    }

    /**
     * @param groupId the groupId to set
     */
    public void setGroupId(String groupId) {
        this.groupId = groupId;
    }

    /**
     * @return the artifactId
     */
    public String getArtifactId() {
        return artifactId;
    }

    /**
     * @param artifactId the artifactId to set
     */
    public void setArtifactId(String artifactId) {
        this.artifactId = artifactId;
    }

}
